<template>
  <div class="lin-container">
    <div class="lin-wrap">
      <el-form ref="formRules" label-width="120px" :model="form" :rules="formRules">
        <el-form-item label="产品类型" prop="parentId">
          <el-select v-model="form.parentId" placeholder="请选择" @change="handleCurrentChange">
            <el-option
              v-for="item in selectList"
              :label="item.indexTitle"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="indexName">
          <el-input v-model="form.indexName"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="indexContent">
          <el-input type="textarea" v-model="form.indexContent"></el-input>
        </el-form-item>
        <el-form-item label="上传图片" class="uploadimage">
          <upload-imgs
            :queryId="queryId"
            ref="uploadEle"
            :rules="rules"
            :imageFileList="imageFileList"
            @setForm="getForm"
          />
        </el-form-item>
        <el-form-item label="排序" prop="indexSort">
          <el-input v-model="form.indexSort"></el-input>
        </el-form-item>
      </el-form>
      <div class="action">
        <el-button @click="reset">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </div>

    <el-image-viewer
      hide-on-click-modal
      @close="()=>{showViewer = false}"
      v-if="showViewer && viewFileList.length>0"
      :url-list="viewFileList"
      :initial-index="0"
    />
  </div>
</template>

<script>
import UploadImgs from "@/components/UploadImg/index.vue";
import {
  menuGetModuleContent,
  deleteMenu,
  addOrUpdateMenu
} from "@/api/PC-Home.js";
import { ElMessage, ElMessageBox } from "element-plus";
export default {
  name: "ImgsUploadStage",
  components: {
    UploadImgs
  },
  data() {
    return {
      form: {
        parentId: "",
        indexName: "",
        indexContent: "",
        indexSort: ""
      },
      formRules: {
        indexName: [
          { required: true, message: "请输入文章标题", trigger: "blur" }
        ],
        indexContent: [
          { required: true, message: "请输入文章内容", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "请输入选择类型", trigger: "change" }
        ],
        indexSort: [{ required: true, message: "请输入排序", trigger: "blur" }]
      },
      dialogTitle: "",
      dialogFormVisible: false,
      formLabelWidth: "140px",
      rules: {
        maxSize: 1,
        minWidth: 100,
        minHeight: 100
      },
      tableData: [],
      showViewer: false,
      viewFileList: [],
      queryId: null,
      file: null,
      imageFileList: [],
      selectList: []
    };
  },
  mounted() {
    this.getQueryId();
    this.getSelectList();
  },
  methods: {
    returnImage(url) {
      return process.env.VUE_APP_URL + "/" + url;
    },
    getQueryId() {
      this.queryId = this.$route.query.queryId;
    },
    showBigImage(url) {
      this.viewFileList = [];
      this.showViewer = true;
      this.viewFileList.push(process.env.VUE_APP_URL + "/" + url);
    },
    getForm(value) {
      console.log(value);
      this.file = value;
    },
    reset() {
      this.menuGetModuleContent();
    },
    submit() {
      this.$refs.formRules.validate(validate => {
        if (validate) {
          let file = this.file;
          let form = null;
          form = new FormData();
          form.append("file", file ? file.raw : "");
          form.append("parentId", this.form.parentId + "003");
          form.append("indexName", this.form.indexName);
          form.append("indexContent", this.form.indexContent);
          form.append("indexSort", this.form.indexSort);
          form.append("id", this.form.id ? this.form.id : "");
          addOrUpdateMenu(form).then(res => {
            if (res.code === 200) {
              this.$message.success("上传成功");
              this.dialogFormVisible = false;
            } else {
              this.$message.error(res.message);
            }
          });
        }
      });
    },
    handleCurrentChange(val) {
      this.form.indexName = "";
      this.form.indexContent = "";
      this.form.indexSort = "";
      this.form.id = "";
      this.imageFileList = [];
      let param = {
        id: val + "003"
      };
      menuGetModuleContent(param).then(res => {
        if (res.data && res.data.length > 0) {
          this.form.indexName = res.data[0].indexName;
          this.form.indexContent = res.data[0].indexContent;
          this.form.indexSort = res.data[0].indexSort;
          this.form.id = res.data[0].id;
          this.imageFileList.push({
            name: res.data[0].indexName,
            url: process.env.VUE_APP_URL + "/" + res.data[0].indexImg
          });
        }
      });
    },
    getSelectList() {
      let param = {
        id: 802
      };
      menuGetModuleContent(param).then(res => {
        this.selectList = res.data;
      });
    },
    handleDelete(item) {
      ElMessageBox.confirm("是否删除该条信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let param = {
            id: item.id
          };
          deleteMenu(param).then(res => {
            if (res.code === 200) {
              ElMessage({
                type: "success",
                message: "删除成功"
              });
              this.menuGetModuleContent();
            }
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消删除"
          });
        });
    }
  }
};
</script>

    
<style scoped lang="scss">
.lin-wrap {
  padding: 40px 0;
  .wrap-label {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    .label {
      font-size: 16px;
    }
  }
}
::v-deep .uploadimage {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  .el-form-item__content {
    display: flex;
    flex-direction: column;
    align-items: flex-start;

    .upload-demo {
      display: flex !important;
      flex-direction: row !important;
      align-items: center !important;

      .el-upload {
        margin-right: 10px;
      }
    }
    .demo-image__preview {
      display: flex;
      flex-direction: row;
    }
  }
}
.action {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
